/**
 * Project: WdFiles
 * Date:    2011-2-11
 */
package com.fametal.ui.utils;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.DialogInterface.OnDismissListener;
import android.graphics.Color;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.FrameLayout.LayoutParams;
import android.widget.TextView;

import com.fametal.R;

public class DialogUtils {
    private static final String tag = Log.getTag(DialogUtils.class);
    private static AlertDialog alertDialog = null;

    private DialogUtils() {
    }
    
    public static boolean isDialogShow(){
    	return null!=alertDialog?alertDialog.isShowing():false;
    }
    
    public static void info(Activity activity, String title, String message, Runnable runnable) {
        String theTitle;
        if (TextUtils.isEmpty(title)) {
            theTitle = activity.getString(R.string.info);
        } else {
            theTitle = title;
        }
        showDialog(activity, android.R.drawable.ic_dialog_info, theTitle, message, runnable);
    }

    public static void error(Activity activity, String title, String message, Runnable runnable) {
        String theTitle;
        if (TextUtils.isEmpty(title)) {
            theTitle = activity.getString(R.string.error);
        } else {
            theTitle = title;
        }
        showDialog(activity, android.R.drawable.ic_dialog_alert, theTitle, message, runnable);
    }

    public static AlertDialog warn(Activity activity, String title, String message, Runnable runnable) {
        String theTitle;
        if (TextUtils.isEmpty(title)) {
            theTitle = activity.getString(R.string.warn);
        } else {
            theTitle = title;
        }
        return showDialog(activity, android.R.drawable.ic_dialog_alert, theTitle, message, runnable);
    }
    public static AlertDialog warnExt(Activity activity, String title, String message, Runnable runnable) {
        String theTitle;
        if (TextUtils.isEmpty(title)) {
            theTitle = activity.getString(R.string.warn);
        } else {
            theTitle = title;
        }
        return showDialogExt(activity, android.R.drawable.ic_dialog_alert, theTitle, message, runnable);
    }
    public static AlertDialog alert(Activity activity, String title, String message, Runnable runnable) {
        String theTitle;
        if (TextUtils.isEmpty(title)) {
            theTitle = activity.getString(android.R.string.dialog_alert_title);
        } else {
            theTitle = title;
        }
        return showDialog(activity, android.R.drawable.ic_dialog_alert, theTitle, message, runnable);
    }
    
    public static AlertDialog showDialog(final Activity activity, Integer iconResId, String title, String message, final Runnable runnable) {
        OnClickListener clickListener = new OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                if (runnable != null) {
                    activity.runOnUiThread(runnable);
//                    ThreadPool.excuteShortTask(runnable);
                }
            }
        };
        OnDismissListener dismissListener = new OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                if (Log.DEBUG.get()) {
                    Log.d(tag, "showDialog Dismiss!");
                }
                if(dialog!= null) {
                	dialog.dismiss();
                }
            }
        };

        removeDialog(activity);
        Builder builder = getBuilder(activity);
        if (iconResId != null) {
            builder.setIcon(iconResId.intValue());
        } else {
            builder.setIcon(android.R.drawable.ic_dialog_info);
        }

        if (title != null) {
            builder.setTitle(title);
        }
        if (message != null) {
            builder.setMessage(message);
        }
        builder.setPositiveButton(R.string.ok, clickListener);

        alertDialog = builder.create();
        alertDialog.setOnDismissListener(dismissListener);

        alertDialog.show();
        return alertDialog;
    }
    public static AlertDialog showDialogExt(final Activity activity, Integer iconResId, String title, String message, final Runnable runnable) {
        OnClickListener clickListener = new OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                if (runnable != null) {
                    activity.runOnUiThread(runnable);
//                    ThreadPool.excuteShortTask(runnable);
                }
            }
        };
        OnDismissListener dismissListener = new OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                if (Log.DEBUG.get()) {
                    Log.d(tag, "showDialogExt Dismiss!");
                }
                if(dialog!= null) {
                	dialog.dismiss();
                }
            }
        };
        removeDialog(activity);
        Builder builder = getBuilder(activity);
        if (iconResId != null) {
            builder.setIcon(iconResId.intValue());
        } else {
            builder.setIcon(android.R.drawable.ic_dialog_info);
        }

        if (title != null) {
            builder.setTitle(title);
        }

        builder.setPositiveButton(R.string.ok, clickListener);

        alertDialog = builder.create();
        TextView tv = new TextView(activity);
        tv.setSingleLine(false);
	   	tv.setText(message);
	   	tv.setTextSize(16);
	   	tv.setPadding(16, 9, 16, 9);
	   	tv.setTextColor(Color.WHITE);
	   	tv.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

	   	alertDialog.setView(tv);
        alertDialog.setOnDismissListener(dismissListener);

        alertDialog.show();
        return alertDialog;
    }
    
    public static void confirm(Activity activity, String title, String message, final Runnable yesTask, final Runnable noTask) {
        confirm(activity, null, title, message, yesTask, noTask, -1, -1);
    }
    
    //add 2 params for custom button text
    public static void confirm(Activity activity, Integer iconResId, String title, String message, final Runnable yesTask, final Runnable noTask,final int but_yes_res,final int but_no_res) {
        OnClickListener clickListener = new OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                if (DialogInterface.BUTTON_POSITIVE == which) {
                    if (yesTask != null) {
                        ThreadPool.excuteShortTask(yesTask);
                    }
                } else {
                    if (noTask != null) {
                        ThreadPool.excuteShortTask(noTask);
                    }
                }
            }
        };
        OnDismissListener dismissListener = new OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                if (Log.DEBUG.get()) {
                    Log.d(tag, "confirm Dismiss!");
                }
                if(dialog!= null) {
                	dialog.dismiss();
                }
            }
        };

        removeDialog(activity);
        
        Builder builder = getBuilder(activity);
        if (iconResId != null) {
            builder.setIcon(iconResId.intValue());
        } else {
            builder.setIcon(android.R.drawable.ic_dialog_info);
        }

        if (title != null) {
            builder.setTitle(title);
        }
		
        if (message != null) {
            builder.setMessage(message);
        }
		
        if (but_yes_res > 0) {
        	builder.setPositiveButton(but_yes_res, clickListener);
        } else {
	        builder.setPositiveButton(R.string.yes, clickListener);
        }
        
		if (but_no_res > 0) {
			builder.setNegativeButton(but_no_res, clickListener);
		} else {
			builder.setNegativeButton(R.string.no, clickListener);
		}
        
        alertDialog = builder.create();
        alertDialog.setOnDismissListener(dismissListener);
        alertDialog.show();
    }

    public static Dialog makeConfirmDialog(Activity activity, String message,
            DialogInterface.OnClickListener okListener,
            DialogInterface.OnClickListener cancelListener) {
    	removeDialog(activity);
        return getBuilder(activity)
            .setIcon(android.R.drawable.ic_dialog_alert)
            .setTitle(android.R.string.dialog_alert_title)
            .setMessage(message)
            .setPositiveButton(R.string.ok, okListener)
            .setNegativeButton(R.string.cancel, cancelListener)
            .create();
    }

    public static Dialog makeContentMenuDialog(Activity activity, String title, String[] msgs, OnClickListener listener) {
    	removeDialog(activity);
    	alertDialog = getBuilder(activity).setIcon(android.R.drawable.ic_dialog_info)
        .setTitle(title).setItems(msgs, listener)
        .create();

    	return alertDialog;
    }

    public static Dialog makeExceptionsDialog(final Activity activity, String title, String[] msgs, final Runnable runnable) {
    	removeDialog(activity);
    	String theTitle;
        if (TextUtils.isEmpty(title)) {
            theTitle = activity.getString(android.R.string.dialog_alert_title);
        } else {
            theTitle = title;
        }

        OnClickListener clickListener = new OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                if (runnable != null) {
                    activity.runOnUiThread(runnable);
                }
            }
        };
        OnDismissListener dismissListener = new OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                if (Log.DEBUG.get()) {
                    Log.d(tag, "makeExceptionsDialog Dismiss!");
                }
                if(dialog!= null) {
                	dialog.dismiss();
                }
            }
        };

        Builder builder = getBuilder(activity).setIcon(android.R.drawable.ic_dialog_alert)
    	.setTitle(theTitle).setPositiveButton(R.string.ok, clickListener);
        if (msgs.length == 1) {
        	builder.setMessage(msgs[0]);
        } else {
        	builder.setItems(msgs, null);
        }
        
    	alertDialog = builder.create();
    	alertDialog.setOnDismissListener(dismissListener);
    	return alertDialog;
    }

    public static Dialog makeConfirmDialogEditText(Activity activity, String message,
            DialogInterface.OnClickListener okListener,
            DialogInterface.OnClickListener cancelListener) {
    		removeDialog(activity);
    		alertDialog = getBuilder(activity)
            .setIcon(android.R.drawable.ic_dialog_alert)
            .setTitle(android.R.string.dialog_alert_title)
            .setMessage(message)
            .setPositiveButton(R.string.ok, okListener)
            .setNegativeButton(R.string.cancel, cancelListener)
            .create();
	    	 EditText tv = new EditText(activity);
	    	 tv.setId(1222);
	    	 tv.setTextSize(16);
	    	 tv.setPadding(16, 9, 16, 9);
	    	 tv.setTextColor(Color.RED);
	    	 tv.setSingleLine(false);
	    	 alertDialog.setView(tv, 30, 0, 30, 20);

    	return alertDialog;
    }
    
    public static Dialog makeConfirmDialogExt(Activity activity, String message,
            DialogInterface.OnClickListener okListener,
            DialogInterface.OnClickListener cancelListener) {
    		removeDialog(activity);
    		alertDialog = getBuilder(activity)
            .setIcon(android.R.drawable.ic_dialog_alert)
            .setTitle(android.R.string.dialog_alert_title)
            //.setMessage(message)
            .setPositiveButton(R.string.ok, okListener)
            .setNegativeButton(R.string.cancel, cancelListener)
            .create();
	    	 TextView tv = new TextView(activity);
	    	 tv.setText(message);
	    	 tv.setTextSize(16);
	    	 tv.setPadding(16, 9, 16, 9);
	    	 tv.setTextColor(Color.WHITE);
	    	 tv.setSingleLine(false);
	    	 tv.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

	    	 alertDialog.setView(tv);

    	return alertDialog;
    }

    public static void removeDialog(final Activity activity) {
    	if (alertDialog != null && alertDialog.getContext().equals(activity)) {
			if (alertDialog.isShowing()) {
				alertDialog.dismiss();
			}
			alertDialog = null;
    	}
    }
    
    // Just use for notification, it's dangerous to clear all, please use the method above.
    public static void clearDialogs() {
    	if (alertDialog != null) {
			if (alertDialog.isShowing()) {
				alertDialog.dismiss();
			}
			alertDialog = null;
    	}
    }

	private static Builder getBuilder(Activity activity) {
//		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
//			return new android.app.AlertDialog.Builder(activity, AlertDialog.THEME_HOLO_DARK);
//		} else {
			return new android.app.AlertDialog.Builder(activity);
//		}
	}
}